//
// Copyright (c) 2024 BPer
//
#ifndef __BPerEncoderMediacodec__
#define __BPerEncoderMediacodec__
#include <bperencoder/BPerVideoEncoder.h>
#include <bperutil/buffer/BPerVideoBuffer.h>
#include <bperutil/buffer/BPerVideoEncoderBuffer.h>
#include <bperutil/sys/BPerLoadLib.h>
#include <bperavutil/video/BPerYuvConvert.h>

#if BPer_OS_ANDROID

#include <media/NdkMediaCodec.h>

class BPerEncoderMediacodec: public BPerVideoEncoder {
public:
	BPerEncoderMediacodec();
	~BPerEncoderMediacodec(void);

	int32_t init(BPerContext* pcontext,BPerVideoInfo* pvideoInfo);

	void setVideoMetaData(BPerVideoMeta *pvmd);
	void parseRtmpHeader(uint8_t *p, int32_t pLen, int32_t *pwid, int32_t *phei, int32_t *pfps);
	void sendMsgToEncoder(BPerRtcEncoderMessage *msg);
private:
	AMediaCodec* m_mediaCodec;
	BPerYuvConvert m_yuv;
	int32_t m_sendKeyframe;
	int32_t m_width;
	int32_t m_height;
	int32_t m_yuvLen;
	int32_t m_colorSpace;
	int32_t encode(BPerFrame* pframe, BPerEncoderCallback* pcallback);
protected:


	void saveFile(char *fileName, uint8_t *pBuffer, int32_t BufferLen);

private:
	BPerSample m_sps;
	BPerSample m_pps;
	int32_t metaLen;
	bool metaInit;
	bool m_hasHeader;
};
#endif

#endif
